package com.sky.service.impl;

import ch.qos.logback.core.joran.util.beans.BeanUtil;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishFlovrMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCarMapper;
import com.sky.service.ShoppingCarService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.beans.beancontext.BeanContext;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCarServiceImpl implements ShoppingCarService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlovrMapper dishFlovrMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private ShoppingCarMapper shoppingCarMapper;

    /**
     * 添加购物车
     *
     * @param shoppingCartDTO
     */
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        //1.判空判断该用户是否有购物车


        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        //只能查询自己的购物车数据
        shoppingCart.setUserId(BaseContext.getCurrentId());
//判断当前商品是否在购物车中
        List<ShoppingCart> shoppingCarts = shoppingCarMapper.selectlist(shoppingCart);
        if (shoppingCarts != null && shoppingCarts.size() == 1) {
            //如果已经存在，就更新数量，数量加1
            shoppingCart = shoppingCarts.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            shoppingCarMapper.updateNumberById(shoppingCart);
        } else {
            //如果不存在，插入数据，数量就是1
            //2.补充缺少属性
            //判断当前添加到购物车的是菜品还是套餐
            if (shoppingCartDTO.getDishId() != null) {
                //添加到购物车的是菜品
                Dish dish = dishMapper.selectById(shoppingCartDTO.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());

            } else {
                //添加到购物车的是套餐
                Setmeal setmeal = setmealMapper.selectById(shoppingCartDTO.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setImage(setmeal.getImage());

            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());

            //3.将商品数据存入到shopping_car表中
            shoppingCarMapper.insert(shoppingCart);
        }


        //最终目的：将用户添加的商品存入到购物车中
    }

    /**
     * 查看购物车
     *
     * @return
     */
    @Override
    public List<ShoppingCart> select() {
        //只能查看自己的购物车商品
        List<ShoppingCart> shoppingCartList = shoppingCarMapper.select(BaseContext.getCurrentId());
        return shoppingCartList;
    }

    @Override
    public void delete() {
        shoppingCarMapper.delete(BaseContext.getCurrentId());
    }

    @Override
    public void deleteBy(ShoppingCartDTO shoppingCartDTO) {
        //查询当前用户的购物车商品数量
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        List<ShoppingCart> shoppingCarts = shoppingCarMapper.selectlist(shoppingCart);
        if (shoppingCarts != null && shoppingCarts.size() > 0) {
            shoppingCart = shoppingCarts.get(0);
            if (shoppingCart.getNumber() == 1) {
                //当前商品在购物车中的份数为1，直接删除当前记录
                shoppingCarMapper.deleteby(shoppingCart.getId());
            } else {
                //当前商品在购物车中的份数不为1，修改份数即可
                shoppingCart.setNumber(shoppingCart.getNumber() - 1);
                shoppingCarMapper.updateNumberById(shoppingCart);
            }

        }
    }

}